<script setup>
import { ref, computed } from 'vue'

const username = ref('')
const password = ref('')
const loading = ref(false)
const errorMsg = ref('')

// 表单验证
const isValid = computed(() => {
  return username.value.trim() !== '' && password.value.trim() !== ''
})

// 防抖处理
let timer = null
const handleLogin = async () => {
  if (!isValid.value) {
    errorMsg.value = '用户名和密码不能为空'
    return
  }

  if (loading.value) return

  loading.value = true
  errorMsg.value = ''

  if (timer) {
    clearTimeout(timer)
  }

  timer = setTimeout(async () => {
    try {
      // 这里添加实际的登录逻辑
      console.log('登录信息：', {
        username: username.value,
        password: password.value
      })
      
      // 模拟登录请求
      await new Promise(resolve => setTimeout(resolve, 1000))
      
      alert('登录成功！')
    } catch (error) {
      errorMsg.value = error.message || '登录失败，请重试'
    } finally {
      loading.value = false
    }
  }, 300)
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2>用户登录</h2>
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            id="username"
            v-model="username"
            type="text"
            placeholder="请输入用户名"
            :class="{ 'error': errorMsg && !username.trim() }"
          >
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            v-model="password"
            type="password"
            placeholder="请输入密码"
            :class="{ 'error': errorMsg && !password.trim() }"
          >
        </div>

        <div v-if="errorMsg" class="error-message">
          {{ errorMsg }}
        </div>

        <button
          type="submit"
          :disabled="!isValid || loading"
          :class="{ 'loading': loading }"
        >
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: fixed;
  top: 0;
  left: 0;
  margin: 0;
  padding: 0;
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  padding: 2.5rem;
  border-radius: 16px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 420px;
  backdrop-filter: blur(10px);
  transform: translateY(0);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
}

h2 {
  text-align: center;
  color: #2d3748;
  margin-bottom: 2.5rem;
  font-size: 1.8rem;
  font-weight: 600;
  letter-spacing: -0.5px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.8rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

label {
  font-size: 0.95rem;
  color: #4a5568;
  font-weight: 500;
  margin-left: 0.2rem;
}

input {
  padding: 0.9rem 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: white;
  color: #2d3748;
}

input::placeholder {
  color: #a0aec0;
}

input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

input.error {
  border-color: #fc8181;
  background-color: #fff5f5;
}

input.error:focus {
  box-shadow: 0 0 0 3px rgba(252, 129, 129, 0.1);
}

.error-message {
  color: #e53e3e;
  font-size: 0.875rem;
  margin-top: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.error-message::before {
  content: "⚠️";
  font-size: 1rem;
}

button {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 1rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 0.5rem;
  position: relative;
  overflow: hidden;
}

button::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

button:hover:not(:disabled)::after {
  opacity: 1;
}

button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.25);
}

button:active:not(:disabled) {
  transform: translateY(0);
}

button:disabled {
  background: #cbd5e0;
  cursor: not-allowed;
  transform: none;
}

button.loading {
  position: relative;
  cursor: wait;
}

button.loading::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 480px) {
  .login-box {
    padding: 2rem;
  }

  h2 {
    font-size: 1.5rem;
    margin-bottom: 2rem;
  }

  input {
    padding: 0.8rem;
  }
}
</style> 